$(function() {
    openUrl(1);
    left()
});

function save() {
    if (lastpick == "") {
        layui.alert("请先选择部门")
    }
    var value = getNodeValue();
    $.ajax({
        type: "post",
        url: "/admin/rbac/saveRoles.aspx",
        dataType: "json",
        async: false,
        data: {
            "roles": value,
            "deptid": lastpick
        },
        success: function(data) {
            layui.alert(data.state)
        },
        error: function(o, a, e) {
            layui.alert("失败")
        }
    })
}
function left() {
    $.ajax({
        type: "post",
        url: "/admin/rbac/getDepts.aspx",
        dataType: "json",
        async: false,
        success: function(data) {
            json = data.deptlist;
            lt = json.length;
            for (x in json) {
                $("#dept").append("<li onclick='openUrl(" + json[x].id + ")' class='c2'   id=i" + json[x].id + ">" + json[x].name + "</li>")
            }
        },
        error: function(o, a, e) {
            layui.alert("失败")
        }
    });
    $.fn.zTree.init($("#treeDemo"), setting, zNodes);
    setCheck();
    $("#py").bind("change", setCheck);
    $("#sy").bind("change", setCheck);
    $("#pn").bind("change", setCheck);
    $("#sn").bind("change", setCheck)
}
function setCheck() {
    zTree = $.fn.zTree.getZTreeObj("treeDemo"), py = $("#py").attr("checked") ? "p" : "", sy = $("#sy").attr("checked") ? "s" : "", pn = $("#pn").attr("checked") ? "p" : "", sn = $("#sn").attr("checked") ? "s" : "", type = {
        "Y": py + sy,
        "N": pn + sn
    };
    zTree.setting.check.chkboxType = type;
    showCode('setting.check.chkboxType = { "Y" : "' + type.Y + '", "N" : "' + type.N + '" };')
}
function showCode(str) {
    if (!code) code = $("#code");
    code.empty();
    code.append("<li>" + str + "</li>")
}
function getNodeValue() {
    var nodes = zTree.getCheckedNodes();
    var s = '';
    for (var i = 0; i < nodes.length; i++) {
        s += nodes[i].id + ","
    }
    return s
}
function openUrl(i) {
    lastpick = i;
    $.ajax({
        type: "post",
        url: "/admin/rbac/getRoles.aspx?deptid=" + i,
        dataType: "json",
        async: false,
        data: {},
        success: function(data) {
            zNodes = eval(data.list);
            $(".c2").removeClass('sel');
            $("#i" + i).addClass('sel')
        },
        error: function(o, a, e) {
            layui.alert("失败")
        }
    });
    $.fn.zTree.init($("#treeDemo"), setting, zNodes)
}
var lt;
var lastpick;
var code;
var zTree;
var zNodes = [];
var setting = {
    autoCheckTrigger: false,
    check: {
        enable: true
    },
    data: {
        simpleData: {
            enable: true
        }
    }
};
setting.check.chkboxType = {
    "Y": "",
    "N": ""
}